//
//  UIListItem.cpp
//  storm
//
//  Created by wangdi on 13-8-18.
//
//

#include "UIListItem.h"
#include "UIList.h"
using namespace cocos2d;

namespace storm
{
    UIListItem *UIListItem::Create(const char *szBack,bool bUseFrame)
    {
        UIListItem *pItem = UIListItem::create();
        pItem->InitState(szBack, bUseFrame);
        return pItem;
    }
    
    //--------------------------------------------------------------------------------------------------------------/
    UIWidget *UIListItem::onReceiveNewTouch(CCTouch *pTouch)
    {
        UIWidget *pRet = UIPanel::onReceiveNewTouch(pTouch);
        if(pRet != NULL) return pRet;
        
        if(isContainsTouch(pTouch))
        {
            if(NULL != m_pParentList) m_pParentList->ItemTouchBegin(pTouch);
            return this;
        }
    
        return NULL;
    }
    
    void UIListItem::onCurTouchMoving(cocos2d::CCTouch *pTouch)
    {
        if(NULL != m_pParentList) m_pParentList->ItemTouchMove(pTouch);
    }
    
    //--------------------------------------------------------------------------------------------------------------/
    void UIListItem::onCurTouchFinish(CCTouch *pTouch)
    {
        CCPoint ptDiff = ccpSub(pTouch->getLocationInView(), pTouch->getStartLocationInView());
        if(fabsf(ptDiff.x) < 5.0f && fabsf(ptDiff.y) < 5.0f)
        {
            OnSelected.emit(this);
        }
        
        if(NULL != m_pParentList) m_pParentList->ItemTouchEnd(pTouch);
    }
    
    
    //-----------------------------------------------------------------------------------------------------------/
    void UIListItem::InitState(const char *szBack, bool bUseFrame)
    {
        if(NULL != szBack)
        {
            if(bUseFrame)
            {
                m_pSprBack = CCSprite::createWithSpriteFrameName(szBack);
            }
            else
            {
                m_pSprBack = CCSprite::create(szBack);
            }
            
            addChild(m_pSprBack);
            
        }
    }
    
    void UIListItem::SetParentList(storm::UIList *pList)
    {
        m_pParentList = pList;
        if(pList->Type() == UIList::LIST_FROMLEFT || pList->Type() == UIList::LIST_FROMRIGHT)
        {
            m_size = m_pSprBack->getContentSize().width;
        }
        else
        {
            m_size = m_pSprBack->getContentSize().height;
        }
        
        setContentSize(m_pSprBack->getContentSize());
    }
    
    //-------------------------------------------------------------------------------------------------------------/
    void UIListItem::SetChangeItem(const char *szBack,bool bUseFrame)
    {
        if(NULL != szBack)
        {
            if(bUseFrame)
            {
                m_pSprChange = CCSprite::createWithSpriteFrameName(szBack);
            }
            else
            {
                m_pSprChange = CCSprite::create(szBack);
            }
            
            m_pSprChange->setVisible(false);
            addChild(m_pSprChange);
        }
    }
    
    //-------------------------------------------------------------------------------------------------------------/
    void UIListItem::AddControlToNormal(UIWidget *pNewControl, cocos2d::CCPoint ptPos, int zOrder)
    {
        pNewControl->setPosition(ptPos);
        addChild(pNewControl, zOrder);
        m_listControlNormal.push_back(pNewControl);
        m_listWidgets.push_back(pNewControl);
    }
    
    //------------------------------------------------------------------------------------------------------------/
    void UIListItem::AddControlToChange(UIWidget *pNewControl, cocos2d::CCPoint ptPos, int zOrder)
    {
        pNewControl->setPosition(ptPos);
        addChild(pNewControl, zOrder);
        m_listControlChange.push_back(pNewControl);
    }
    
    //-----------------------------------------------------------------------------------------------------------/
    void UIListItem::Active(bool bActive)
    {
        m_bActive =bActive;
        m_listWidgets.clear();
        if(bActive)
        {
            for(WidgetList::iterator iter = m_listControlNormal.begin(); iter != m_listControlNormal.end(); iter++)
            {
                (*iter)->setVisible(false);
            }
            
            for(WidgetList::iterator iter = m_listControlChange.begin(); iter != m_listControlChange.end(); iter++)
            {
                (*iter)->setVisible(true);
                m_listWidgets.push_back(*iter);
            }
            
        }
        else
        {
            for(WidgetList::iterator iter = m_listControlChange.begin(); iter != m_listControlChange.end(); iter++)
            {
                (*iter)->setVisible(false);
            }
            
            for(WidgetList::iterator iter = m_listControlNormal.begin(); iter != m_listControlNormal.end(); iter++)
            {
                (*iter)->setVisible(true);
                m_listWidgets.push_back(*iter);
            }
        }
        if(NULL != m_pSprChange)
        {
            m_pSprChange->setVisible(bActive);
            m_pSprBack->setVisible(!bActive);
        }
    }
 
}